package per.hx.bs.yw.serviceimpl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import per.hx.bs.base.domain.BaseMap;
import per.hx.bs.base.domain.Tree;
import per.hx.bs.entity.yw.YwAppraisalItemDO;
import per.hx.bs.entity.yw.YwAppraisalTypeDO;
import per.hx.bs.util.BuildTree;
import per.hx.bs.yw.dao.YwAppraisalItemDao;
import per.hx.bs.yw.dao.YwAppraisalTypeDao;

import java.util.*;

/**
 * @author HuXing
 * @date 2018/4/14  11:13
 */
@Transactional(rollbackFor = Exception.class)
@RestController
@RequestMapping(value = "yw-service/appraisal")
public class YwAppraisalApiServiceImpl {

    private final
    YwAppraisalTypeDao appraisalTypeDao;
    private final
    YwAppraisalItemDao appraisalItemDao;

    @Autowired
    public YwAppraisalApiServiceImpl(YwAppraisalTypeDao appraisalTypeDao, YwAppraisalItemDao appraisalItemDao) {
        this.appraisalTypeDao = appraisalTypeDao;
        this.appraisalItemDao = appraisalItemDao;
    }


    /**
     * 获取指标
     *
     * @param type 通过一级指标获取列表
     * @return 指标集合
     */
    @GetMapping(value = "/list")
    public List<YwAppraisalItemDO> list(@RequestParam("type") String type) {
        EntityWrapper<YwAppraisalTypeDO> ewType = new EntityWrapper<>();
        ewType.setEntity(new YwAppraisalTypeDO(type));
        ewType.orderBy("serial_no", true);
        List<YwAppraisalTypeDO> typeDOList = appraisalTypeDao.selectList(ewType);
        if (typeDOList != null) {
            EntityWrapper<YwAppraisalItemDO> ewItem = new EntityWrapper<>();
            ewItem.setEntity(new YwAppraisalItemDO(type));
            ewItem.orderBy("serial_no", true);
            List<YwAppraisalItemDO> itemDOList = appraisalItemDao.selectList(ewItem);
            for (YwAppraisalTypeDO typeDO : typeDOList) {
                itemDOList.add(new YwAppraisalItemDO(typeDO.getId(), typeDO.getAppraisalName(),
                        typeDO.getAppraisalValue(),
                        typeDO.getShowId()));
            }
            return itemDOList;
        }
        return null;
    }

    /**
     * 获取一级指标
     *
     * @return 一级指标
     */
    @GetMapping(value = "/type")
    public List<YwAppraisalTypeDO> type() {
        EntityWrapper<YwAppraisalTypeDO> ew = new EntityWrapper<>();
        ew.setEntity(new YwAppraisalTypeDO());
        ew.orderBy("serial_no", true);
        return appraisalTypeDao.selectList(ew);
    }

    /**
     * 保存一级指标
     *
     * @param appraisalTypeDO 一级指标
     * @return 影响行数
     */
    @PostMapping(value = "/type/save")
    public int saveType(@RequestBody YwAppraisalTypeDO appraisalTypeDO) {
        EntityWrapper<YwAppraisalTypeDO> ew = new EntityWrapper<>();
        ew.setEntity(new YwAppraisalTypeDO());
        int showId = appraisalTypeDao.selectCount(ew);
        appraisalTypeDO.setId("type-" + UUID.randomUUID());
        appraisalTypeDO.setShowId(String.valueOf(showId + 1));
        appraisalTypeDO.setSerialNo(showId);
        appraisalTypeDO.setCreateDate(new Date());
        return appraisalTypeDao.insertType(appraisalTypeDO);
    }

    /**
     * 通过id获取 一级指标
     *
     * @param id id
     * @return 一级指标
     */
    @GetMapping(value = "/type/{id}")
    public YwAppraisalTypeDO findTypeById(@PathVariable("id") String id) {
        return appraisalTypeDao.selectById(id);
    }

    /**
     * 修改 一级指标
     *
     * @param typeDO 一级指标
     * @return 影响行数
     */
    @PutMapping(value = "/type/update")
    public int updateType(@RequestBody YwAppraisalTypeDO typeDO) {
        typeDO.setUpdateDate(new Date());
        return appraisalTypeDao.updateById(typeDO);
    }

    /**
     * 删除一级指标
     *
     * @param id 指标id
     * @return 影响行数
     */
    @DeleteMapping(value = "/type/delete/{id}")
    public int deleteType(@PathVariable("id") String id) {
        EntityWrapper<YwAppraisalItemDO> ew = new EntityWrapper<>();
        ew.setEntity(new YwAppraisalItemDO());
        ew.where("parent_id={0}", id);
        if (appraisalItemDao.selectCount(ew) > 0) {
            return 0;
        } else {
            return appraisalTypeDao.deleteById(id);
        }

    }

    /**
     * 通过id获取 二级指标
     *
     * @param id id
     * @return 二级指标
     */
    @GetMapping(value = "/item/{id}")
    public YwAppraisalItemDO findItemById(@PathVariable("id") String id) {
        return appraisalItemDao.selectById(id);
    }

    /**
     * 保存二级指标
     *
     * @param itemDO 二级指标
     * @return 影响行数
     */
    @PostMapping(value = "/item/save")
    public int saveItem(@RequestBody YwAppraisalItemDO itemDO) {
        EntityWrapper<YwAppraisalItemDO> ew = new EntityWrapper<>();
        ew.setEntity(new YwAppraisalItemDO());
        itemDO.setId("item-" + UUID.randomUUID());
        ew.where("parent_id={0}", itemDO.getParentId());
        Integer showCount = appraisalItemDao.selectCount(ew);
        itemDO.setCreateDate(new Date());
        itemDO.setSerialNo(showCount);
        try {
            itemDO.setShowId(itemDO.getShowId() + '.' + (showCount + 1));
            return appraisalItemDao.insertItem(itemDO);
        } catch (Exception e) {
            itemDO.setShowId(itemDO.getShowId() + '.' + showCount);
            return appraisalItemDao.insertItem(itemDO);
        }
    }

    /**
     * 修改 二级指标
     *
     * @param itemDO 一级指标
     * @return 影响行数
     */
    @PutMapping(value = "/item/update")
    public int updateItem(@RequestBody YwAppraisalItemDO itemDO) {
        itemDO.setUpdateDate(new Date());
        return appraisalItemDao.updateById(itemDO);
    }

    /**
     * 删除二级指标
     *
     * @param id 指标id
     * @return 影响行数
     */
    @DeleteMapping(value = "/item/delete/{id}")
    public int deleteItem(@PathVariable("id") String id) {
        EntityWrapper<YwAppraisalItemDO> ew = new EntityWrapper<>();
        ew.setEntity(new YwAppraisalItemDO());
        ew.where("parent_id={0}", id);
        if (appraisalItemDao.selectCount(ew) > 0) {
            return 0;
        } else {
            return appraisalItemDao.deleteById(id);
        }

    }

    /**
     * 检查是否存在id
     *
     * @param id 指标id
     * @return true/false
     */
    @GetMapping(value = "/item/hasChild/{id}")
    public boolean itemHasChild(@PathVariable("id") String id) {
        return appraisalItemDao.selectByMap(BaseMap.putV("parent_id", id)).isEmpty();
    }

    /**
     * 通过ParentId获取绩效津贴考核二级指标
     *
     * @param pId ParentId
     * @return item集合
     */
    @GetMapping(value = "/item/parentId/{pId}")
    public List<YwAppraisalItemDO> selectByParentId(@PathVariable("pId") String pId) {
        return appraisalItemDao.selectByMap(BaseMap.putV("parent_id", pId));
    }

    /**
     * 获取 指标树
     *
     * @param type 一级指标
     * @return 指标
     */
    @GetMapping(value = "/appraisalTree")
    public String getTree(@RequestParam("type") String type) {
        List<Tree<YwAppraisalItemDO>> trees = new ArrayList<>();
        List<YwAppraisalItemDO> itemDOS = list(type);
        for (YwAppraisalItemDO itemDO : itemDOS) {
            Tree<YwAppraisalItemDO> tree = new Tree<>();
            tree.setId(itemDO.getId());
            tree.setParentId(itemDO.getParentId());
            tree.setText(itemDO.getAppraisalContent());
            Map<String, Object> state = new HashMap<>(1);
            state.put("opened", true);
            state.put("mType", "appraisal");
            tree.setState(state);
            trees.add(tree);
        }
        return JSON.toJSONString(BuildTree.build(trees, "考核指标选择"));
    }
}
